home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 681 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
  2. Message-ID: <4i062e$ji@mulga.cs.mu.OZ.AU>
  3. X-Original-Date: 11 Mar 1996 03:18:38 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 11 Mar 96 03:20:25 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: delete or delete[] ???
  9. Organization: Comp Sci, University of Melbourne
  10. References: <64ayxNJ3cDB@faerber.muc.de>
  11. Keywords: delete
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMUOcCOEDnX0m9pzZAQH1VgF/R0nLy3hfURI+fYKIOkdWCGtZpFWROHFE
  14.     5zs/SQSs8xKyuSroi0FWrItAzJkmFd7H
  15.     =o1UN
  16.  
  17. claus@faerber.muc.de (Claus A. Faerber) writes:
  18.  
  19. >David Sachs <b91926@fsgi01.fnal.gov> (07 Mar 96):
  20. >
  21. >> Should delete or delete[] be used for a pointer to an array type?
  22. >>
  23. >> // It is assumed that some_type is an existing type
  24. >> typedef some_type array_type[10];
  25. >>
  26. >> array_type* a1 = new array_type[5];
  27. >> array_type* b1 = new some_type[5][10];
  28. >
  29. >In both cases, you're creating a some_type[5][10], which is  
  30. >an array_type[5] by typedef.
  31.  
  32. The fact that a typedef is used is irrelevant.
  33.  
  34. >It is an array of array_types, sou you have to use delete[].
  35.  
  36. Correct, because it is an array, you have to use `delete[]'.
  37.  
  38. >> array_type* a2 = new array_type;
  39. >> array_type* b2 = new some_type[10];
  40. >
  41. >Here, you are creating a some_type[10] in both cases, which  
  42. >is array_type.
  43. >It is one single array_type, not an array of array_types, so  
  44. >you have to use delete without [] in both cases.
  45.  
  46. No, that's incorrect.  It is, as you say, "one single array_type", but
  47. `array_type' is an array type, so `delete[]' must be used.
  48. See 5.3.5[expr.delete]/2.
  49.  
  50. >> delete   a2; // or should it be delete[] a2;
  51. >
  52. >Ok, points to a single array_type.
  53.  
  54. No, that's incorrect, for the same reason as above.
  55.  
  56. >> delete[] b2; // or should it be delete   b2;
  57. >
  58. >*No*, points also to a single array_type, so you actually  
  59. >have to use delete b2.
  60.  
  61. Again, that's incorrect, for the same reason as above.
  62.  
  63. --
  64. Fergus Henderson                 WWW: http://www.cs.mu.oz.au/~fjh
  65. fjh@cs.mu.oz.au                  PGP: finger fjh@128.250.37.3
  66. ---
  67. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  68. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  69. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  70. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  71. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  72.